package com.yanyan.sannuomin.result;

import lombok.*;


@Data
@NoArgsConstructor
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> {
    private Boolean success;    //是否成功
    private String code;       //返回状态码
    private String message;     //返回信息
    private Long count;      //数据数量
    private T data;             //返回数据


    //创建ResultBean对象，接收数据
    protected static<T>  Result<T> build(T data){
//        System.out.println("build中："+data);
        //实例化Result对象
        Result<T> tResult = new Result<>();
        if(data!=null){
            tResult.setData(data);
        }
        return tResult;
    }

    /**
     * 成功
     * @param resultCode
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(ResultCode resultCode,T data){
//        System.out.println("ok中："+data);
        Result<T> result = build(data);
        result.setSuccess(true);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMassage());
        return result;
    }

    public static<T> Result<T> ok(ResultCode resultCode,long count,T data){
        Result<T> result = build(data);
        result.setSuccess(true);
        result.setCode(resultCode.getCode());
        result.setCount(count);
        result.setMessage(resultCode.getMassage());
        return result;
    }

    /**
     * 失败
     * @param resultCode
     * @param data
     * @return
     */
    public static<T> Result<T> fail(ResultCode resultCode,T data){
        Result<T> result = build(data);
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMassage());
        return result;
    }

    /**
     * 失败
     */
    public static Result error(ResultCode errorInfo) {
        Result result = new Result();
        result.setCode(errorInfo.getResultCode());
        result.setMessage(errorInfo.getResultMassage());
        return result;
    }

    /**
     * 失败
     */
    public static Result error(String code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败
     */
    public static Result error( String message) {
        Result result = new Result();
        result.setCode("-1");
        result.setMessage(message);
        return result;
    }
}

